#include "VideoTest.h"

int main(int argc, char* argv[])
{
    vidInit();
    GLEngine* engine = new GLEngine("VideoTest - F1 to load video", 800, 600, 32, false, 0, 0);
    engine->createClient<VideoTest>("videotest");
    engine->runClient("videotest");
    delete engine;
}


//
// SceneCinema
//

SceneCinema::SceneCinema(SceneGraph* scene) :
    SceneObject(scene)
{
    video = NULL;
    texFrame = NULL;
}

SceneCinema::~SceneCinema()
{
    vidClose(video);
    delete texFrame;
}

void SceneCinema::onUpdate(float dt)
{
    if (vidUpdate(video, dt))
        vidRewind(video);
}

void SceneCinema::onRender()
{
    VidFrame frm;
    vidGetFrame(video, &frm);
    if (!texFrame)
        texFrame = new GLTexture(frm.width, frm.height, GLTEX_EDGE);
    texFrame->update(frm.data);

    glColor(Color::White);
    glTranslatef(0, 0, 4.5);
    glRotatef(90, 1, 0, 0);
    glRenderSprite(texFrame, 16, 9, false, false, true);
}

void SceneCinema::playVideo(const char* filename)
{
    vidClose(video);
    video = vidOpen(filename, 0, 0);
    delete texFrame;
    texFrame = NULL;
}


//
// VideoTest
//

VideoTest::VideoTest(GLEngine* engine) :
    GLClient(engine)
{
    camera.zoom = -30;
    //camera.position.set(50, 50, 0);
    camera.rotation.set(-70, 0, 30);

    SceneBox* floor;
    SceneRagdoll_Old* user;

    scene = new SceneGraph(engine);

    floor = scene->createObject<SceneBox>();
    floor->setScale(30, 40, 0.5);
    floor->setPosition(0, -20, -0.5);

    for (int x = -10; x < 10; x++)
    {
        for (int y = -5; y > -40; y--)
        {
            user = scene->createObject<SceneRagdoll_Old>();
            user->setPosition(x, y, fabs(y) * 0.1);
            user->setRotationZ(-90);
        }
    }

    cinema = scene->createObject<SceneCinema>();
    cinema->setPosition(0, 0, 1);
    cinema->playVideo("test1.avi");
}

VideoTest::~VideoTest()
{
}

void VideoTest::onEnter()
{
}

void VideoTest::onLeave()
{
}

void VideoTest::onEvent(GLEvent e)
{
    switch (e.type)
    {
    case EVT_KeyPress:
        {
            if (e.key.code == KEY_ESC)
            {
                getEngine()->quit(0);
            }
            else if (e.key.code == KEY_F1)
            {
                char fn[1024];
                if (show_open_box(fn, "AVI files|*.avi"))
                {
                    cinema->playVideo(fn);
                }
            }
        }
        break;
    case EVT_MouseMove:
        {
            int dx = e.mouse.x - mouse.x;
            int dy = e.mouse.y - mouse.y;
            if (mouse.button[MOUSE_RIGHT])
            {
                camera.rotation.z += dx;
                camera.rotation.x += dy;
            }
            if (mouse.button[MOUSE_LEFT])
            {
                camera.translate2D(-dx*0.1, dy*0.1);
            }
        }
        break;
    case EVT_MouseWheel:
        {
            camera.zoom -= e.mouse.wheel;
        }
        break;
    }
}

void VideoTest::onUpdate(float dt)
{
    scene->update(dt);
}

void VideoTest::onDraw()
{
    glBeginScene3D();
    camera.apply();
    scene->render();
    glEndScene3D();
}





